package org.example.mutithread.future;

import cn.hutool.core.date.DateUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author zhangjg
 * @desc f1
 * @date 2022/09/20 16:00
 **/
public class F1 {

    public static void main(String[] args) {

        Runnable runnable = () -> {
            System.out.println("无返回结果异步任务-start");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("无返回结果异步任务-end");
        };
        CompletableFuture.runAsync(runnable);

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("有返回值的异步任务-start");
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("有返回值的异步任务-end");
            return "Hello World";
        });

        try {
            System.out.println("----stime="+ DateUtil.now());
            String result = future.get();
            System.out.println("----etime="+ DateUtil.now()+" result="+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }


}
